package cn.ad.service;

import cn.ad.entity.BaseReportValue;
import cn.ad.handler.SqlSessionHandler;
import cn.ad.model.BaseRptModel;
import cn.ad.model.IBaseRptModel;
import cn.ad.model.StrategyGroupRptTimePeriod;
import cn.ad.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class StrategyGroupRptService {

	@Autowired
	protected SqlSessionHandler sqlSessionHandler;
	
	private BaseRptModel getStrategyGroupRptTimePeriod(StrategyGroupRptTimePeriod condition) {
		
		if(condition.getStartTime()==null||condition.getEndTime()==null){
			throw new IllegalArgumentException("开始时间和结束时间不能为null。数据错误。");
		}
		BaseRptModel result = new BaseRptModel();
		condition.setStartDaily(Utils.getStartTime(condition.getStartTime()));
		condition.setEndDaily(Utils.getStartTime(condition.getEndTime()));
		Date currentDateStartTime = Utils.getStartTime(Calendar.getInstance().getTime());
		if(currentDateStartTime.getTime()<condition.getStartDaily().getTime()){
			return new BaseRptModel();
		}
		//当开始时间是今天
		if(currentDateStartTime.getTime()==condition.getStartDaily().getTime()){
			condition.setCurrentDaily(currentDateStartTime);
			BaseReportValue baseRptValue = sqlSessionHandler.selectOne("rptCpnStrategyGroupHourlyMapper.selectSum", condition);
			if(baseRptValue!=null){
				result.setClick(baseRptValue.getClick());
				result.setPv(baseRptValue.getPv());
				result.setCost(baseRptValue.getCost());
			}
			return result;
		}
		
		//当开始时间是今天之前。结束时间是今天之前
		if(currentDateStartTime.getTime()>condition.getEndDaily().getTime()){
			BaseReportValue baseRptValue = sqlSessionHandler.selectOne("rptCpnStrategyGroupDailyMapper.selectSum", condition);
			if(baseRptValue!=null){				
				result.setClick(baseRptValue.getClick());
				result.setPv(baseRptValue.getPv());
				result.setCost(baseRptValue.getCost());
			}
			return result;
		}
		
		//当开始时间是今天之前。结束时间是今天或今天之后
		if(currentDateStartTime.getTime()<=condition.getEndDaily().getTime()){
			//算整天的
			BaseReportValue baseRptValue = sqlSessionHandler.selectOne("rptCpnStrategyGroupDailyMapper.selectSum", condition);
			if(baseRptValue!=null){				
				result.setClick(baseRptValue.getClick());
				result.setPv(baseRptValue.getPv());
				result.setCost(baseRptValue.getCost());
			}
			//算今天的
			condition.setCurrentDaily(currentDateStartTime);
			baseRptValue = sqlSessionHandler.selectOne("rptCpnStrategyGroupHourlyMapper.selectSum", condition);
			if(baseRptValue!=null){				
				result.setClick(result.getClick()+baseRptValue.getClick());
				result.setPv(result.getPv()+baseRptValue.getPv());
				result.setCost(result.getCost()+baseRptValue.getCost());
			}
			return result;
		}
		return result;
	}

	public IBaseRptModel getStrategyGroupRptTimePeriod(int campaignId, List<Integer> strategyId, Date startTime,
													   Date endTime) {
		if(null == strategyId || strategyId.size() == 0) {
			return new BaseRptModel();
		}
		StrategyGroupRptTimePeriod condition= new StrategyGroupRptTimePeriod();
		condition.setCampaignId(campaignId);
		condition.setStrategyIds(strategyId);
		condition.setStartTime(startTime);
		condition.setEndTime(endTime);
		return getStrategyGroupRptTimePeriod(condition);
	}

}
